package com.meituan.catering.management.shop.biz.model.converter;

import com.meituan.catering.management.shop.api.http.model.request.SearchShopHttpRequest;
import com.meituan.catering.management.shop.biz.model.ShopBO;
import com.meituan.catering.management.shop.biz.model.common.UserContextBO;
import com.meituan.catering.management.shop.biz.model.enumeration.DescribableEnum;
import com.meituan.catering.management.shop.biz.model.enumeration.EnabledEnum;
import com.meituan.catering.management.shop.dao.mapper.ShopMapper;
import com.meituan.catering.management.shop.dao.model.ShopDO;
import com.meituan.catering.management.shop.dao.model.request.SearchShopDataRequest;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.List;
import java.util.Set;

/**
 * @Author:ale
 * @Date 2022/4/22 17:05
 * @Version 1.0
 */
public abstract class ShopDOConverter {

    public static SearchShopDataRequest buildDataRequest(SearchShopHttpRequest request) {
        SearchShopDataRequest dataRequest = new SearchShopDataRequest();

        // Condition
        SearchShopHttpRequest.Condition condition = request.getCondition();
        dataRequest.setSkip((request.getPageIndex() - 1) * request.getPageSize());
        dataRequest.setSize(request.getPageSize());
        dataRequest.setBusinessType(condition.getBusinessType());
        dataRequest.setManagementType(condition.getManagementType());
        EnabledEnum enabled = DescribableEnum.getByProperty(EnabledEnum.class, EnabledEnum::getBooleanValue, condition.getEnabled());
        if (enabled != null) {
            dataRequest.setEnabled(enabled.getCode());
        }
        if (StringUtils.isNotBlank(condition.getKeyword())) {
            dataRequest.setHasKeywords(true);
            dataRequest.setName(MessageFormat.format("%{0}%", condition.getKeyword()));
            dataRequest.setContactTelephone(MessageFormat.format("%{0}%", condition.getKeyword()));
            dataRequest.setContactCellphone(MessageFormat.format("%{0}%", condition.getKeyword()));
            dataRequest.setContactAddress(MessageFormat.format("%{0}%", condition.getKeyword()));
            dataRequest.setContactName(MessageFormat.format("%{0}%", condition.getKeyword()));
        }else {
            dataRequest.setHasKeywords(false);

        }
        // SortField
        List<SearchShopHttpRequest.SortField> sortField = request.getSortFields();
        if (sortField != null) {
            String field = sortField.get(0).getField();
            Boolean asc = sortField.get(0).getAsc();
            if (asc) {
                dataRequest.setSortByClause(field + " " + "asc");
            } else {
                dataRequest.setSortByClause(field + " " + "desc");
            }
        }
        return dataRequest;
    }

    public static SearchShopDataRequest fromRequest(UserContextBO userContext, SearchShopHttpRequest request, Set<Long> privilegeShopIds) {
        SearchShopDataRequest dataRequest = new SearchShopDataRequest();

        // Condition
        SearchShopHttpRequest.Condition condition = request.getCondition();
        dataRequest.setSkip((request.getPageIndex() - 1) * request.getPageSize());
        dataRequest.setSize(request.getPageSize());
        dataRequest.setBusinessType(condition.getBusinessType());
        dataRequest.setManagementType(condition.getManagementType());
        EnabledEnum enabled = DescribableEnum.getByProperty(EnabledEnum.class, EnabledEnum::getBooleanValue, condition.getEnabled());
        dataRequest.setPrivilegedShopIds(privilegeShopIds);
        if (enabled != null) {
            dataRequest.setEnabled(enabled.getCode());
        }
        if (StringUtils.isNotBlank(condition.getKeyword())) {
            dataRequest.setHasKeywords(true);
            dataRequest.setName(MessageFormat.format("%{0}%", condition.getKeyword()));
            dataRequest.setContactTelephone(MessageFormat.format("%{0}%", condition.getKeyword()));
            dataRequest.setContactCellphone(MessageFormat.format("%{0}%", condition.getKeyword()));
            dataRequest.setContactAddress(MessageFormat.format("%{0}%", condition.getKeyword()));
            dataRequest.setContactName(MessageFormat.format("%{0}%", condition.getKeyword()));
        }else {
            dataRequest.setHasKeywords(false);

        }
        // SortField
        List<SearchShopHttpRequest.SortField> sortField = request.getSortFields();
        if (sortField != null) {
            String field = sortField.get(0).getField();
            Boolean asc = sortField.get(0).getAsc();
            if (asc) {
                dataRequest.setSortByClause(field + " " + "asc");
            } else {
                dataRequest.setSortByClause(field + " " + "desc");
            }
        }
        return dataRequest;
    }

}
